var ps;

function initPFD() {
	try {
		setInterval("drawPFD()", 100);
	} catch (e) {
		alert(e);
	}
}

function drawPFD() {
	var canvas = document.getElementById('pfd');
	
	canvas.width = canvas.width;
	ps = canvas.height/1000.0;

	// Make sure we don't execute when canvas isn't supported
	if(canvas.getContext) {
		var ctx = canvas.getContext('2d');
		pfdAltBar(ctx);
		pfdAtt(ctx);
		pfdSpeedbar(ctx);
		
		if (!plane_gps_valid && plane_pitch == null && plane_roll == null)
		{
			ctx.beginPath();
			ctx.strokeStyle = "#F00";
			ctx.lineWidth = 50*ps;
			ctx.moveTo(0,0);
			ctx.lineTo(1000*ps,1000*ps);
			ctx.moveTo(1000*ps,0);
			ctx.lineTo(0,1000*ps);
			ctx.stroke();
		}
	}
	else
	{
		alert("No PFD");
	}
}

function pfdAtt(ctx)
{
	try
	{
		ctx.save();//crop
		var top,left,right,bottom,corner,rng;
		corner = 100*ps;
		top = 200*ps;
		left = 200*ps;
		right = 700*ps;
		bottom = 800*ps;
		rng = 60;//*ps;
		
		ctx.beginPath();
		ctx.moveTo(left,top+corner);
		ctx.lineTo(left,bottom-corner);
		ctx.quadraticCurveTo(left,bottom,left+corner,bottom);
		ctx.lineTo(right-corner,bottom);
		ctx.quadraticCurveTo(right,bottom,right,bottom-corner);
		ctx.lineTo(right,top+corner);
		ctx.quadraticCurveTo(right,top,right-corner,top);
		ctx.lineTo(left+corner,top);
		ctx.quadraticCurveTo(left,top,left,top+corner);
		ctx.clip();
		
		//bs the pitch and roll
		var pitch,roll;
		pitch = plane_pitch;
		roll = plane_roll;
		if (pitch==null){pitch=0;}
		if (roll==null){roll=0;}
		var y = _map(pitch,0,rng/2,0,(bottom-top)/2)
		
		ctx.save();//skyline
		
		//ground and sky
		var height = (90+(rng/2))/rng;
		ctx.translate(0,((top+bottom)/2)+y);
		ctx.fillStyle = "blue";
		ctx.fillRect(left,0,right,-(bottom-top)*height);
		ctx.fillStyle = "brown";
		ctx.fillRect(left,0,right,(bottom-top)*height);
		
		//lines
		ctx.beginPath();
		ctx.strokeStyle = "white";
		ctx.lineWidth = 5*ps;
		ctx.moveTo(left,0);
		ctx.lineTo(right,0);
		ctx.stroke();
		ctx.translate((left+right)/2,0);
		
		var yy = (bottom-top)/rng;
		var x;
		
		ctx.font = (30*ps)+"pt Helvetica";
	    ctx.textBaseline = "middle";
	    ctx.fillStyle = "white";
		
		for (var d=2.5;d<=90+(rng/2);d+=2.5)
		{
			y = yy*d;
			x = 20*ps;
			if (d%5==0){x = 50*ps;}
			if (d%10==0)
			{
				x = 100*ps;
				var txt = d;
				if (txt>90){txt=180-txt;}
				ctx.textAlign = "left";
				ctx.fillText(txt,x,y);
				ctx.fillText(txt,x,-y);
				ctx.textAlign = "right";
				ctx.fillText(txt,-x,y);
				ctx.fillText(txt,-x,-y);
			}
			
			//up
			ctx.moveTo(-x,-y);
			ctx.lineTo(x,-y);
			
			//down
			ctx.moveTo(-x,y);
			ctx.lineTo(x,y);
		}
		ctx.stroke();//draw the lines for the pitch guage
		
		ctx.restore();//~skyline
		
		ctx.save();//attitude icon
		var r = (bottom-top)/2;
		ctx.translate((left+right)/2,(top+bottom)/2);
		
		//but first, the degree markers
		for (var d=-40;d<=40;d+=10)
		{
			ctx.save();
			ctx.beginPath();
			ctx.rotate(deg2rad(d+180));
			ctx.moveTo(0,r*0.8);
			if (d%20==0){ctx.lineTo(0,r);}
			else {ctx.lineTo(0,r*0.9);}
			ctx.strokeStyle = "black";
			ctx.lineWidth = 10*ps;
			ctx.stroke();
			ctx.strokeStyle = "white";
			ctx.lineWidth = 5*ps;
			ctx.stroke();
			
			ctx.restore();
		}
		ctx.save();//ARROW
		ctx.rotate(deg2rad(roll+180));
		ctx.beginPath();
		ctx.moveTo(0,r*0.8);
		ctx.lineTo(r*0.05,r*0.7);
		ctx.lineTo(-r*0.05,r*0.7);
		ctx.closePath();
		ctx.strokeStyle = "black";
		ctx.lineWidth = 5*ps;
		ctx.fillStyle = "white";
		ctx.fill();
		ctx.stroke();
		ctx.restore();//~ARROW
		//end degree markers
		
		
		ctx.rotate(deg2rad(roll));
		ctx.beginPath();
		ctx.strokeStyle = "white";
		ctx.fillStyle = "black";
		ctx.lineWidth = 10*ps;
		ctx.moveTo(0,0);
		ctx.lineTo(40*ps,50*ps);
		ctx.lineTo(100*ps,50*ps);
		ctx.lineTo(0,0);
		ctx.lineTo(-100*ps,50*ps);
		ctx.lineTo(-40*ps,50*ps);
		ctx.closePath();
		ctx.fill();
		ctx.stroke();
		ctx.restore();//~attitude icon
		
		if (plane_pitch == null && plane_roll == null && plane_gps_valid)
		{
			//cross out the whole thing
			ctx.beginPath();
			ctx.strokeStyle = "red";
			ctx.lineWidth = 50*ps;
			ctx.moveTo(left,top);
			ctx.lineTo(right,bottom);
			ctx.moveTo(right,top);
			ctx.lineTo(left,bottom);
			ctx.stroke();
		}
		
		ctx.restore();//~crop
		
		
		ctx.font = (70*ps)+"pt Helvetica";
		ctx.textAlign = "left";
	    ctx.textBaseline = "bottom";
	    ctx.fillStyle = "red";
		if (!(plane_speed == null && plane_alt == null))
		{
			if (plane_pitch == null)
			{
				if (plane_roll != null)
				{
					ctx.fillText("PITCH INOP",left,top);
				}
				//case covered for both right above
			}
			else if (plane_roll == null)
			{
				ctx.fillText("ROLL INOP",left,top);
			}
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function pfdAltBar(ctx)
{
	try
	{
		var alt;//ground level or sea level readouts
		alt = plane_alt;
		if (pfdPrefGround){alt-=groundLevel;}
		
		
		ctx.save();//CLIP
		ctx.beginPath();
		ctx.fillStyle = "#777";
		var left,right,top,bottom;
		left = 750*ps;
		right = 990*ps;
		top = 80*ps;
		bottom = 800*ps;
		ctx.fillRect(left, top, right, bottom);
		
		//the clipper
		ctx.beginPath();
		ctx.moveTo(left,bottom+top);
		ctx.lineTo(left,top);
		ctx.lineTo(right,top);
		ctx.lineTo(right,bottom+top);
		ctx.closePath();
		ctx.clip();
		//end clipper
		
		ctx.font = (30*ps)+"pt Helvetica";
	    ctx.textAlign = "left";
	    ctx.textBaseline = "middle";
	    
	    var intervals = 10;
	    var rng = 100;
	    
	    var bar_top = alt+(rng/2);
	    var bar_bottom = alt-(rng/2);
	    var bar_width = 30*ps;
	    
	    var upper,lower;
	    upper = Math.ceil(bar_top/intervals)*intervals;
	    lower = (Math.floor(bar_bottom/intervals)-1)*intervals;
	    
	    ctx.strokeStyle = "white";
	    ctx.fillStyle = "white";
	    	   	    
	    for (var i = upper; i>=lower; i-=intervals)
		{
			if (i<0){break;}//do not draw marks below 0 feet
			//markers for the different altitudes
			ctx.save();//ALT MARK
			ctx.translate(0,_map(i,bar_top,bar_bottom,top,bottom));
			ctx.beginPath();
			ctx.moveTo(left,0);
			if (i%100==0)
			{
				ctx.lineWidth = 10*ps;
				ctx.lineTo(left+(bar_width*2),0);
			}
			else
			{
				ctx.lineWidth = 5*ps;
				ctx.lineTo(left+bar_width,0);
			}
			ctx.stroke();
			ctx.fillText(i,left+bar_width+(50*ps),0);
			ctx.restore();//~ALT MARK
		}
		
		ctx.restore();//~CLIP
				
		//display the text for altitude
		ctx.save();//ALT CLIP
		
		ctx.translate(0,(top+bottom)/2);
		//ctx.fillRect(0,0,100,100);
		ctx.beginPath();
		ctx.moveTo(right,50*ps);
		ctx.lineTo(left+bar_width,50*ps);
		ctx.lineTo(left+bar_width,20*ps);
		ctx.lineTo(left,0);
		ctx.lineTo(left+bar_width,-20*ps);
		ctx.lineTo(left+bar_width,-50*ps);
		ctx.lineTo(right,-50*ps);
		ctx.closePath();
		ctx.fillStyle = "black";
		ctx.fill();
		ctx.strokeStyle = "white";
		ctx.lineWidth = 10*ps;
		ctx.stroke();
		ctx.clip();
		
		//draw the altitude inside
		ctx.font = (40*ps)+"pt Helvetica";
	    ctx.textAlign = "right";
	    ctx.textBaseline = "middle";
	    ctx.fillStyle = "white";
	    
	    var y=0,center = (left+right-bar_width)/2, calt = Math.max(alt,0);
	    if (calt%100/10>9){y = _map(calt%100,90,100,0,80*ps);}
	    repr = Math.floor(calt/100);
		ctx.fillText(repr,center+(20*ps),y);
		ctx.fillText(repr+1,center+(20*ps),y-(80*ps));
		
		y = _map(alt%10,0,10,0,50*ps);
		var talt = Math.floor(calt/10)*10;
		ctx.fillText(_alt(talt),center+(80*ps),y);
		ctx.fillText(_alt(talt+10),center+(80*ps),y-(50*ps));
		ctx.fillText(_alt(talt+20),center+(80*ps),y-(100*ps));
		ctx.fillText(_alt(talt-10),center+(80*ps),y+(50*ps));
				
		ctx.restore();//~ALT CLIP
		
		ctx.font = (40*ps)+"pt Helvetica";
	    ctx.textAlign = "center";
	    ctx.textBaseline = "bottom";
	    ctx.fillStyle = "white";
	    ctx.fillText(plane_alt_method,(left+right)/2,top);
	    
	    if (plane_alt == null && !(plane_pitch == null && plane_roll == null))
		{
			ctx.beginPath();
			ctx.strokeStyle = "#F00";
			ctx.lineWidth = 20*ps;
			ctx.moveTo(left,top);
			ctx.lineTo(right,bottom);
			ctx.moveTo(right,top);
			ctx.lineTo(left,bottom);
			ctx.stroke();
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function _alt(n)
{
	n %= 100;
	if (n==-10)
	{
		return 90;
	}
	if (n==0)
	{
		return "00";
	}
	return n;
}

function pfdSpeedbar(ctx)
{
	try
	{
		ctx.save();//CLIP
		ctx.beginPath();
		ctx.fillStyle = "#777";
		var left,right,top,bottom;
		left = 10*ps;
		right = 150*ps;
		top = 80*ps;
		bottom = 800*ps;
		ctx.fillRect(left, top, right, bottom);
		
		//the clipper
		ctx.beginPath();
		ctx.moveTo(left,bottom+top);
		ctx.lineTo(left,top);
		ctx.lineTo(right,top);
		ctx.lineTo(right,bottom+top);
		ctx.closePath();
		ctx.clip();
		//end clipper
		
		ctx.font = (30*ps)+"pt Helvetica";
	    ctx.textAlign = "right";
	    ctx.textBaseline = "middle";
	    
	    var intervals = 2;
	    var rng = 10;
	    
	    var bar_top = plane_speed+(rng/2);
	    var bar_bottom = plane_speed-(rng/2);
	    var bar_width = 30*ps;
	    
	    var upper,lower;
	    upper = Math.ceil((plane_speed+(rng/2))/intervals)*intervals;
	    lower = Math.floor((plane_speed-(rng/2))/intervals)*intervals;
	    
	    ctx.strokeStyle = "white";
	    ctx.fillStyle = "white";
	    	   	    
	    for (var i = upper; i>=lower; i-=intervals)
		{
			//markers for the different speeds
			ctx.save();//SPEED MARK
			ctx.translate(0,_map(i,bar_top,bar_bottom,top,bottom));
			ctx.beginPath();
			ctx.moveTo(right,0);
			if (i%10==0)
			{
				ctx.lineWidth = 10*ps;
				ctx.lineTo(right-(bar_width*3/2),0);
			}
			else
			{
				ctx.lineWidth = 5*ps;
				ctx.lineTo(right-bar_width,0);
			}
			ctx.stroke();
			ctx.fillText(i,right-bar_width-(20*ps),0);
			ctx.restore();//~SPEED MARK
		}
		
		ctx.restore();//~CLIP
				
		//display the text for speed
		ctx.save();//SPD CLIP
		
		ctx.translate(0,(top+bottom)/2);
		//ctx.fillRect(0,0,100,100);
		ctx.beginPath();
		ctx.moveTo(left,50*ps);
		ctx.lineTo(right-bar_width,50*ps);
		ctx.lineTo(right-bar_width,20*ps);
		ctx.lineTo(right,0);
		ctx.lineTo(right-bar_width,-20*ps);
		ctx.lineTo(right-bar_width,-50*ps);
		ctx.lineTo(left,-50*ps);
		ctx.closePath();
		ctx.fillStyle = "black";
		ctx.fill();
		ctx.strokeStyle = "white";
		ctx.lineWidth = 10*ps;
		ctx.stroke();
		ctx.clip();
		
		//draw the speed inside
		ctx.font = (40*ps)+"pt Helvetica";
	    ctx.textAlign = "right";
	    ctx.textBaseline = "middle";
	    ctx.fillStyle = "white";
	    var y=0,center = (left+right-bar_width)/2;
	    if (plane_speed%10>9){y = _map(plane_speed%10,9,10,0,80*ps);}
		ctx.fillText(Math.floor(plane_speed/10),center,y);
		ctx.fillText(Math.ceil(plane_speed/10),center,y-(80*ps));
		y = _map((plane_speed*10)%10,0,10,0,50*ps);
		ctx.fillText(Math.floor(plane_speed)%10,center+(30*ps),y);
		ctx.fillText(Math.ceil(plane_speed)%10,center+(30*ps),y-(50*ps));
		ctx.fillText(Math.ceil(plane_speed+1)%10,center+(30*ps),y-(100*ps));
		ctx.fillText(Math.floor(plane_speed-1)%10,center+(30*ps),y+(50*ps));
				
		ctx.restore();//~SPD CLIP
		
		ctx.font = (40*ps)+"pt Helvetica";
	    ctx.textAlign = "center";
	    ctx.textBaseline = "bottom";
	    ctx.fillStyle = "white";
	    ctx.fillText(plane_speed_method,(left+right)/2,top);
	    
	    if (plane_speed == null && !(plane_pitch == null && plane_roll == null))
		{
			ctx.beginPath();
			ctx.strokeStyle = "#F00";
			ctx.lineWidth = 20*ps;
			ctx.moveTo(left,top);
			ctx.lineTo(left+right,top+bottom);
			ctx.moveTo(left+right,top);
			ctx.lineTo(left,top+bottom);
			ctx.stroke();
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function _map(x, in_min, in_max, out_min, out_max)
{
 	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}